#include "StdAfx.h"
#include "KLineManager.h"
#include "GraphKLine.h"
#include "GraphAssist.h"
#include <string>

CKLineManager::CKLineManager(void)
{
	m_nManagerType = gManagerKLine;
	CGraphKLine *pKline = new CGraphKLine();
	m_VecDrawObj.push_back(pKline);
	pKline = new CGraphKLine();
	m_VecDrawObj.push_back(pKline);

}

CKLineManager::~CKLineManager(void)
{
	for (size_t i = 0; i < m_VecDrawObj.size(); ++i)
	{
		SAFE_DELETE(m_VecDrawObj[i]);
	}
	m_VecDrawObj.clear();
}

bool CKLineManager::Update()
{
	CXTPChartContent *pContent = ChartControl()->GetContent();
	if (pContent == NULL)
	{
		return false;
	}
	CXTPChartSeries* pSeries;
	std::string strTmp;
	AGraphDateBase* pData = NULL;
	for (size_t i = 0; i < m_VecDrawObj.size(); ++i)
	{
		pSeries = pContent->GetSeries()->Add(new CXTPChartSeries());
		if (pSeries == NULL)
		{
			return false;
		}
		pData = m_VecDrawObj[i]->GetGrapData(strTmp);
		SetDrawPoint(pData, pSeries);
		pSeries->SetStyle(CGraphAssist::GetStyle(pData));
		pSeries->SetValueScaleType(xtpChartScaleNumerical);
		
		SetDrawXLable(pData, pSeries);
		SAFE_DELETE(pData);
	}

	return true;
}

void CKLineManager::SetDrawPoint(AGraphDateBase* pData, CXTPChartSeries* pSeries)
{
	
	std::string strCode = _T("");

	OneObj_Graph graphData;
	if (!CGraphAssist::ConvertOneGraph(pData, graphData))
	{
		return;
	}
	for (size_t j = 0; j < graphData.size(); ++j)
	{
		pSeries->GetPoints()->Add(graphData[j]);
	}
}

void CKLineManager::SetDrawXLable(AGraphDateBase* pData, CXTPChartSeries* pSeries )
{
	OneObj_Lable graphLable;
	
	if (!CGraphAssist::ConvertOneLable(pData, graphLable))
	{
		return;
	}

	CXTPChartDiagram2D* pDiagram = DYNAMIC_DOWNCAST(CXTPChartDiagram2D, pSeries->GetDiagram());
	for (size_t j = 0; j < graphLable.size(); ++j)
	{
		pDiagram->GetAxisX()->GetCustomLabels()->Add(graphLable[j]);
	}
	
	pDiagram->GetAxisX()->GetRange()->SetViewAutoRange(FALSE);;
	pDiagram->GetAxisX()->GetRange()->SetViewMinValue(0);
	pDiagram->GetAxisX()->GetRange()->SetViewMaxValue(8);
	pDiagram->GetAxisY()->GetRange()->SetShowZeroLevel(FALSE);

}

bool CKLineManager::UpdateYRange()
{
	bool bRet = false;
	AGraphDateBase* pData = NULL;
	std::string strTmp;
	std::string strStart;
	std::string strEnd;

	CGraphAssist::GetShowXRange(ChartControl(), strStart, strEnd);

	double dMin = 1.0e10;
	double dMax = -1.010;
	
	for (size_t i = 0; i < m_VecDrawObj.size(); ++i)
	{
		if (m_VecDrawObj[i]->GetValueRange(strTmp, strStart, strEnd, dMin, dMax))
		{
			bRet = true;
		}
	}
	if (!bRet)
	{
		return false;
	}

	CGraphAssist::SetShowYRange(ChartControl(), dMin, dMax);
	return true;
}

bool CKLineManager::OnKeyDown( UINT nChar, UINT nRepCnt, UINT nFlags )
{
	if (nChar == VK_LEFT)
	{
		OnLeft();
	}
	else if (nChar == VK_RIGHT)
	{
		OnRight();
	}
	else if (nChar == VK_UP)
	{
		OnUp();
	}
	else if (nChar == VK_DOWN)
	{
		OnDown();
	}
	return true;
}

void CKLineManager::OnLeft()
{
	double dShowMin = 0;
	double dShowMax = 0;
	double dMin = 0;
	double dMax = 0;
	
	CGraphAssist::GetShowXRange(ChartControl(), dShowMin, dShowMax);
	CGraphAssist::GetXRange(ChartControl(), dMin, dMax);
	if (dShowMin - 1 < dMin)
	{
		return;
	}
	dShowMin -= 1;
	dShowMax -= 1;

	CGraphAssist::SetShowXRange(ChartControl(), dShowMin, dShowMax);

	UpdateYRange();
}

void CKLineManager::OnRight()
{
	double dShowMin = 0;
	double dShowMax = 0;
	double dMin = 0;
	double dMax = 0;

	CGraphAssist::GetShowXRange(ChartControl(), dShowMin, dShowMax);
	CGraphAssist::GetXRange(ChartControl(), dMin, dMax);
	if (dShowMax + 1 > dMax)
	{
		return;
	}
	dShowMin += 1;
	dShowMax += 1;

	CGraphAssist::SetShowXRange(ChartControl(), dShowMin, dShowMax);

	UpdateYRange();
}

void CKLineManager::OnUp()
{
	double dShowMin = 0;
	double dShowMax = 0;
	double dMin = 0;
	double dMax = 0;

	CGraphAssist::GetShowXRange(ChartControl(), dShowMin, dShowMax);
	CGraphAssist::GetXRange(ChartControl(), dMin, dMax);

	CGraphAssist::Zoommin(dMin, dMax, dShowMin, dShowMax);
	/*dShowMin = dShowMin + (dShowMax - dShowMin) / 2.0;
	if (dShowMin > dShowMax - 5)
	{
		dShowMin = dShowMax - 10;
	}

	dShowMin = (double)(int)(dShowMin + 0.5);*/

	CGraphAssist::SetShowXRange(ChartControl(), dShowMin, dShowMax);

	UpdateYRange();

}

void CKLineManager::OnDown()
{
	double dShowMin = 0;
	double dShowMax = 0;
	double dMin = 0;
	double dMax = 0;

	CGraphAssist::GetShowXRange(ChartControl(), dShowMin, dShowMax);
	CGraphAssist::GetXRange(ChartControl(), dMin, dMax);

	CGraphAssist::Zoomout(dMin, dMax, dShowMin, dShowMax);
	/*dShowMin = dShowMin - (dShowMax - dShowMin) / 3.0;
	if (dShowMin < dMin)
	{
		dShowMin = dMin;
	}

	dShowMin = (double)(int)(dShowMin + 0.5);*/

	CGraphAssist::SetShowXRange(ChartControl(), dShowMin, dShowMax);

	UpdateYRange();


}
